﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using remes.BLoc.UI;

namespace remes.BLoc.TestApp
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         Project = null;
         InitializeComponent();
      }

      public BLocProject Project { get; private set; }

      private void OpenProjectButton_Click(object sender, RoutedEventArgs e)
      {
         var dlg = new OpenFileDialog()
         {
            Title = "Open Project",
            Filter = "C# Project file (*.csproj)|*.csproj|All files (*.*)|*.*"
         };
         var result = dlg.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
            BLocProject prj = null;
            try
            {
               prj = new BLocProject(dlg.FileName, false);
            }
            catch (Exception ex)
            {
               prj = null;
               if (MessageBox.Show(this,
                  String.Format("The following erroes occured while opening the Project file:\n{0}\n\n" +
                  "Do you want to try again, apply default settings and open the properties dialog?\n" +
                  "(This will modify the project file!)", ex.Message),
                  "Open Project file", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
               {
                  try
                  {
                     prj = new BLocProject(dlg.FileName, true);
                     
                     var prjDlg = new BLocProjectDialog();
                     prjDlg.Owner = this;
                     prjDlg.Project = prj;
                     prjDlg.ShowDialog();
                  }
                  catch (Exception ex_)
                  {
                     prj = null;
                     MessageBox.Show(this,
                        String.Format("Could not open/modify project file even applying default values:\n{0}", ex_.Message),
                        "Open Project file", MessageBoxButton.OK, MessageBoxImage.Error);
                  }
               }
            }
            if (prj != null)
            {
               ProjectPathTextBox.Text = prj.ProjectPath;
               Project = prj;
               BuildConfigurationsComboBox.Items.Clear();
               foreach (var conf in Project.Configurations)
               {
                  BuildConfigurationsComboBox.Items.Add(conf);
               }
               if (BuildConfigurationsComboBox.Items.Count > 0)
               {
                  BuildConfigurationsComboBox.SelectedIndex = 0;
               }
            }
         }
      }

      private void EditProjectSettingsButton_Click(object sender, RoutedEventArgs e)
      {
         if (!CheckProject()) return;

         var dlg = new BLocProjectDialog();
         dlg.Owner = this;
         dlg.Project = Project;
         dlg.ShowDialog();
      }

      private void CheckUIDsButton_Click(object sender, RoutedEventArgs e)
      {
         if (!CheckProject()) return;

         var stdOutWnd = new StdOutputWindow();
         stdOutWnd.Owner = this;
         stdOutWnd.Description = "Checking Project file for missing UID's";
         stdOutWnd.ExecutionCallback = new StdOutputExecutionDelegate((stdOutputCallback) =>
            {
               Project.CheckUIDs(stdOutputCallback);
            });
         stdOutWnd.ShowDialog();
      }

      private void UpdateUIDsButton_Click(object sender, RoutedEventArgs e)
      {
         if (!CheckProject()) return;
         var stdOutWnd = new StdOutputWindow();
         stdOutWnd.Owner = this;
         stdOutWnd.Description = "Update Project file UID's";
         stdOutWnd.ExecutionCallback = new StdOutputExecutionDelegate((stdOutputCallback) =>
         {
            Project.UpdateUIDs(stdOutputCallback);
         });
         stdOutWnd.ShowDialog();
      }

      private void ParseAssemblyButton_Click(object sender, RoutedEventArgs e)
      {
         if (!CheckProject()) return;
         if (!CheckBuildConfiguration()) return;
         var stdOutWnd = new StdOutputWindow();
         stdOutWnd.Owner = this;
         stdOutWnd.Description = "Parsing assembly";
         var buildConfiguration = SelectedBuildConfiguration;
         stdOutWnd.ExecutionCallback = new StdOutputExecutionDelegate((stdOutputCallback) =>
         {
            Project.ParseBaml(buildConfiguration, stdOutputCallback);
         });
         stdOutWnd.ShowDialog();
      }

      private void TranslateButton_Click(object sender, RoutedEventArgs e)
      {
         if (!CheckProject()) return;
         try
         {
            var editor = new TranslationEditor();
            editor.Owner = this;
            editor.TranslationSet = Project.LoadTranslationSet();
            editor.ShowDialog();
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.Message);
         }
      }

      private void GenerateAssembliesButton_Click(object sender, RoutedEventArgs e)
      {
         if (!CheckProject()) return;
         if (!CheckBuildConfiguration()) return;

         TranslationSet translationSet = null;
         try
         {
            translationSet = Project.LoadTranslationSet();
         }
         catch(Exception ex)
         {
            MessageBox.Show(this, "Loading Translation set:\n" + ex.Message, "Baml Localizer", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
         }
         if (translationSet == null)
         {
            MessageBox.Show(this, "Loading Translation set:\nIs null!?", "Baml Localizer", MessageBoxButton.OK, MessageBoxImage.Error);
         }

         var langDlg = new SelectTargetLanguagesDialog();
         langDlg.Owner = this;
         langDlg.SetAvailableLanguages(translationSet.TargetLanguages);
         var result = langDlg.ShowDialog();
         if (result.HasValue && result.Value)
         {
            var stdOutWnd = new StdOutputWindow();
            stdOutWnd.Owner = this;
            stdOutWnd.Description = "Generating Satelite assemblies";
            var buildConfiguration = SelectedBuildConfiguration;
            stdOutWnd.ExecutionCallback = new StdOutputExecutionDelegate((stdOutputCallback) =>
               {
                  Project.GenerateSateliteAssemblies(buildConfiguration, langDlg.SelectedLanguages, stdOutputCallback);
               });
            stdOutWnd.ShowDialog();
         }
      }

      private BuildConfiguration SelectedBuildConfiguration
      {
         get
         {
            return BuildConfigurationsComboBox.SelectedItem as BuildConfiguration;
         }
      }

      private bool CheckProject()
      {
         if (Project == null)
         {
            MessageBox.Show(this, "There is no project open!", "Baml Localizer", MessageBoxButton.OK, MessageBoxImage.Error);
            return false;
         }
         return true;
      }

      private bool CheckBuildConfiguration()
      {
         if (SelectedBuildConfiguration == null)
         {
            MessageBox.Show(this, "There is no Build configuration selected!", "Baml Localizer", MessageBoxButton.OK, MessageBoxImage.Error);
            return false;
         }
         return true;
      }
   }
}
